Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PSR-6 caching interfce & remove desarolla2 cache #10

Merged
merged 6 commits into from
Nov 9, 2017

Conversation

sunspikes
Copy link
Owner

  • Use PSR-6 caching interfaces
  • Remove Dsarrola cache adapter and use php-cache (for functional tests)

@sunspikes
Copy link
Owner Author

Hi @Feijs

Can you please have a look. So basically i removed Desarrola completely and decoupled the caching part (it now depends only on PSR-6 interfaces), for functional tests I'm using the array cache from php-cache and I've started using PHP 7 typehints.

Now for v2 i think,

  • Add back unit tests for throttling algorithms
  • Need to enforce strict types and type hints in all places (I think nullable typehints and void types would have been better, but for this min version need to be bumped to 7.1)
  • Refactor the throttling algorithms a bit to reduce duplicate code & improve the documentation a bit more

That's what i can think of now, what you think ?

@sunspikes sunspikes changed the base branch from master to 2.x-dev October 27, 2017 14:08
@sunspikes sunspikes added the 2.x label Oct 27, 2017
Copy link
Collaborator

@Feijs Feijs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please have a look. So basically i removed Desarrola completely and decoupled the caching part (it now depends only on PSR-6 interfaces), for functional tests I'm using the array cache from php-cache and I've started using PHP 7 typehints.

Looks good! 👍 I've added a few (mostly minor) notes

Add back unit tests for throttling algorithms

Why have these been removed? I can update them to match your changes in this PR if that is the issue?

  • Need to enforce strict types and type hints in all places (I think nullable typehints and void types would have been better, but for this min version need to be bumped to 7.1)
  • Refactor the throttling algorithms a bit to reduce duplicate code & improve the documentation a bit more

Agreed, perhaps an incremental upgrade to 7.1 would be best, but since active support for 7.0 ends in a month, it it quite reasonable to update to 7.1 in one go

@@ -0,0 +1,3294 @@
<?xml version="1.0" encoding="UTF-8"?>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this should have been submitted?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch!, removed, thanks!

*/
public function getItem(string $key): ThrottlerItemInterface
{
$item = $this->getThrottlerItem($key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be returned inline

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

/**
* @param string $key
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These docblocks (for the public methods) can all be replaced by @inheritdoc

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* @throws CacheAdapterException
* @throws ItemNotFoundException
*/
private function getThrottlerItem(string $key): ThrottlerItemInterface
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both private methods are used only once, so could be moved to the corresponding public method

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks!

/**
* @param string $key
*
* @return ThrottlerItemInterface
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add these exceptions to the interface

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that makes sense, thanks!

$cacheItem = $this->cacheItemPool->getItem($key);

if ($cacheItem->isHit()) {
$this->cacheItemPool->deleteItem($key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the item deleted if it already exists? Is there an issue with updates if the item pre-existed?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, when i tested (with redis) i see the item gets duplicated. this way it works, but may be we can add more real tests

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so i got the problem, it was because all tests were using same key. I've fixed this issue and I removed the delete line. And now the tests are running on redis by default (if available)

/** @var int $count */
private $count;

/** @var int $ttl */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ttl can also be null

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -101,7 +104,9 @@ public function count()
}

try {
$this->counter = $this->cache->get($this->key);
/** @var CacheCount $item */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be a nice improvement to check whether these items contain the expected type:

if (!$item instanceof CacheCount) {
    $this->counter = 0;
}

Theoretically the cache could contain garbage or unsafe data.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps something for a seperate PR though

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this can be done, but in case it is a hit, it should get the actual saved value (best case)


$this->cache->set($this->key, $this->counter, $this->ttl);
$this->cache->setItem($this->key, $item);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The item variable is used only once, so this can be done inline. Applies to a number of similar cases below

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -46,6 +44,7 @@ public function testThrottlePostLimit()
$throttle->hit();
}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

superfluous newline

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@sunspikes
Copy link
Owner Author

sunspikes commented Nov 7, 2017

Why have these been removed? I can update them to match your changes in this PR if that is the issue?

It would be awesome if you can add it back. Now all of them fails because of these new changes, i've tried to fix it but it was not good as i wanted it to be :D

@Feijs Also, thank you so much for your comments!

@sunspikes
Copy link
Owner Author

I will merge this to the release branch, if there is something to be addressed it can be done in a new PR.

@sunspikes sunspikes merged commit 6f5f4d2 into 2.x-dev Nov 9, 2017
@sunspikes sunspikes deleted the feature/psr-6 branch November 9, 2017 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants